"""
@Author  : 康帅
@Time    : 2021/7/3 10:44
@Function: 字典推导式
"""


def dict_derivative():
    """
    字典推导式
    :return:
    """
    mcase = {'a': 10, 'b': 34, 'A': 7, 'Z': 3}

    mcase_frequency = {
        k.lower(): mcase.get(k.lower(), 0) + mcase.get(k.upper(), 0)
        for k in mcase.keys()
    }
    print(mcase_frequency)


def dict_derivative_v2():
    """
    字典推导式
    :return:
    """
    DIAL_CODES = [
        (86, 'China'),
        (91, 'India'),
        (1, 'United States'),
        (62, 'Indonesia'),
        (55, 'Brazil'),
        (92, 'Pakistan'),
        (880, 'Bangladesh'),
        (234, 'Nigeria'),
        (7, 'Russia'),
        (81, 'Japan'),
    ]

    country_code = {country: code for code, country in DIAL_CODES}
    print(country_code)

    # __contains__(k)
    print(country_code.__contains__('Japan'))  # True
    print(country_code.__contains__('ks'))  # False

    # __delitem__(k)
    country_code.__delitem__('Japan')  # 删除Japan元素
    print(country_code)

    # clear() 情况所有元素
    country_code.clear()
    print(country_code)

    # data = {code: country.upper() for country, code in country_code.items() if code < 66}
    # print(data)


def dict_setdefault():
    """
    setdefault
    :return:
    """
    my_dict = {
        'India': 91,
        'United States': 1,
        'Indonesia': 62,
        'Bangladesh': 880,
        'Nigeria': 234,
        'Russia': 7
    }

    # 1.使用setdefault插入字典
    my_dict.setdefault('China', []).append("86")
    print(my_dict)

    # 2.使用遍历方法插入字典
    if "China" not in my_dict:
        my_dict['China'] = []
    my_dict['China'].append(86)
    print(my_dict)


if __name__ == '__main__':
    dict_derivative()
    dict_derivative_v2()
    dict_setdefault()
